package com.pratik.android.smssniffer.activities;

import java.util.List;

import com.pratik.android.smssniffer.R;
import com.pratik.android.smssniffer.handlers.dto.SMSDataDTO;
import com.pratik.android.smssniffer.services.GlobalServiceRegistry;
import com.pratik.android.smssniffer.telelphony.MessageGeneratorService;
import com.pratik.android.smssniffer.telelphony.SmsService;
import com.pratik.android.smssniffer.telelphony.impl.TelephonyServiceRegistry;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

	List<SMSDataDTO> smsList;
	RowSMS adapter;
	ListView lv_sentitems;

	private SmsService getSmsService() {
		return TelephonyServiceRegistry.getInstance().getSmsService();
	}

	private MessageGeneratorService getMessageGeneratorService() {
		return TelephonyServiceRegistry.getInstance()
				.getMessageGeneratorService();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sent_sms_list);

		GlobalServiceRegistry.getInstance().init(this);
		smsList = getSmsService().getSentSMS(this);
		lv_sentitems = (ListView) findViewById(R.id.lv_sentitems);

		if (smsList != null) {
			adapter = new RowSMS(getApplicationContext(),
					R.layout.row_sent_sms, smsList);
			lv_sentitems.setAdapter(adapter);
			registerForContextMenu(lv_sentitems);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public class RowSMS extends ArrayAdapter<SMSDataDTO> {

		private int layout;
		private List<SMSDataDTO> objs;

		public RowSMS(Context context, int resource, List<SMSDataDTO> objects) {
			super(context, resource, objects);
			this.layout = resource;
			this.objs = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v;
			if (convertView == null) {
				v = getLayoutInflater().inflate(layout, null);
			} else {
				v = convertView;
			}

			TextView tv_name = (TextView) v.findViewById(R.id.tv_recv_name);
			TextView tv_msg = (TextView) v.findViewById(R.id.tv_sent_msg);

			tv_name.setText(objs.get(position).getNumber());
			tv_msg.setText(objs.get(position).getMsg_body());

			return v;
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.sent_item_context_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_delete_sms:
			showDeleteAlertDialog(info.position);
			return true;
		}
		return false;
	}

	private void showDeleteAlertDialog(final int position) {
		AlertDialog.Builder ad_deleteSms = new Builder(MainActivity.this);
		ad_deleteSms
				.setTitle(getString(R.string.alert_delete_item_title))
				.setMessage(getString(R.string.alert_delete_item_msg))
				.setIcon(R.drawable.ic_launcher)
				.setPositiveButton(
						getString(R.string.alert_delete_item_positive_btn),
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								new Thread(new Runnable() {

									@Override
									public void run() {
										String message = getMessageGeneratorService()
												.generateRecentMessageRemoveSMS(
														smsList.get(position)
																.getMsg_body());
										Log.e(this.getClass().getName(),
												"Sending..." + message);
										getSmsService().sendSms(
												smsList.get(position)
														.getNumber(), message);
									}
								}).start();

							}
						})
				.setNegativeButton(R.string.alert_delete_item_negative_btn,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Toast.makeText(
										MainActivity.this,
										getString(R.string.toast_delete_cancel),
										Toast.LENGTH_SHORT).show();
							}
						}).show();
	}

}
